/*
 * @(#)MtmWorkflowStepUserActor.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.bean.MtmWorkflowStepUserActor;
import com.dcivision.workflow.bean.WorkflowStep;

/**
 * MtmWorkflowStepUserActorDAObject.java
 *
 * This class is the data access bean for table "MTM_WF_STEP_USER_ACTOR".
 *
 * @author Angus Shiu
 * @company DCIVision Limited
 * @creation date 05/08/2003
 * @version $Revision: 1.34 $
*/

public class MtmWorkflowStepUserActorDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.34 $";

  public static final String TABLE_NAME = "MTM_WF_STEP_USER_ACTOR";

  public MtmWorkflowStepUserActorDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("WORKFLOW_STEP_ID");
    this.vecDBColumn.add("ACTOR_TYPE");
    this.vecDBColumn.add("PERMISSION");
    this.vecDBColumn.add("ACTOR_ID");
    this.vecDBColumn.add("ACTION_TYPE");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.ACTOR_TYPE, A.PERMISSION, A.ACTOR_ID, A.ACTION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          MtmWorkflowStepUserActor tmpMtmWorkflowStepUserActor = new MtmWorkflowStepUserActor();
          tmpMtmWorkflowStepUserActor.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepUserActor.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
          tmpMtmWorkflowStepUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
          tmpMtmWorkflowStepUserActor.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpMtmWorkflowStepUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
          tmpMtmWorkflowStepUserActor.setActionType(getResultSetString(rs, "ACTION_TYPE"));
          tmpMtmWorkflowStepUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getCreatorID()));
          tmpMtmWorkflowStepUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getUpdaterID()));
          return(tmpMtmWorkflowStepUserActor);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.ACTOR_TYPE, A.PERMISSION, A.ACTOR_ID, A.ACTION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          MtmWorkflowStepUserActor tmpMtmWorkflowStepUserActor = new MtmWorkflowStepUserActor();
          tmpMtmWorkflowStepUserActor.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepUserActor.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
          tmpMtmWorkflowStepUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
          tmpMtmWorkflowStepUserActor.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpMtmWorkflowStepUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
          tmpMtmWorkflowStepUserActor.setActionType(getResultSetString(rs, "ACTION_TYPE"));
          tmpMtmWorkflowStepUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getCreatorID()));
          tmpMtmWorkflowStepUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getUpdaterID()));
          tmpMtmWorkflowStepUserActor.setRecordCount(totalNumOfRecord);
          tmpMtmWorkflowStepUserActor.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpMtmWorkflowStepUserActor);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.ACTOR_TYPE, A.PERMISSION, A.ACTOR_ID, A.ACTION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowStepUserActor tmpMtmWorkflowStepUserActor = new MtmWorkflowStepUserActor();
          tmpMtmWorkflowStepUserActor.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepUserActor.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
          tmpMtmWorkflowStepUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
          tmpMtmWorkflowStepUserActor.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpMtmWorkflowStepUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
          tmpMtmWorkflowStepUserActor.setActionType(getResultSetString(rs, "ACTION_TYPE"));
          tmpMtmWorkflowStepUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getCreatorID()));
          tmpMtmWorkflowStepUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getUpdaterID()));
          result.add(tmpMtmWorkflowStepUserActor);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public  synchronized List getListByWorkflowStepIDAndActorType(Integer workflowStepID,String actorType) throws ApplicationException {
    return getListByWorkflowStepIDAndActorTypeAndPermissions(workflowStepID,actorType,null);
  }
  public  synchronized List getListByWorkflowStepIDAndActorTypeAndPermissions(Integer workflowStepID,String actorType,String[] permissions) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.ACTOR_TYPE, A.PERMISSION, A.ACTOR_ID, A.ACTION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.WORKFLOW_STEP_ID = ? AND A.ACTOR_TYPE = ? ");
        if(!Utility.isEmpty(permissions)){
          sqlStat.append(" AND ( ");
          for(int y=0;y<permissions.length;y++){
            if(y==0){
              sqlStat.append(" A.PERMISSION LIKE ? ");
            }else{
              sqlStat.append(" OR A.PERMISSION LIKE ? ");
            }
          }
          sqlStat.append(" ) ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowStepID);
        this.setPrepareStatement(preStat, 3, actorType);
        if(!Utility.isEmpty(permissions)){
          for(int i=0;i<permissions.length;i++){
            String keyWord = this.getFormattedKeyword(permissions[i],"LIKE");
            this.setPrepareStatement(preStat, i+4, keyWord);
          }
        }
        rs = preStat.executeQuery();
        while (rs.next()) {
        MtmWorkflowStepUserActor tmpMtmWorkflowStepUserActor = new MtmWorkflowStepUserActor();
        tmpMtmWorkflowStepUserActor.setID(getResultSetInteger(rs, "ID"));
        tmpMtmWorkflowStepUserActor.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
        tmpMtmWorkflowStepUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
        tmpMtmWorkflowStepUserActor.setPermission(getResultSetString(rs, "PERMISSION"));
        tmpMtmWorkflowStepUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
        tmpMtmWorkflowStepUserActor.setActionType(getResultSetString(rs, "ACTION_TYPE"));
        tmpMtmWorkflowStepUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
        tmpMtmWorkflowStepUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
        tmpMtmWorkflowStepUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
        tmpMtmWorkflowStepUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
        tmpMtmWorkflowStepUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
        tmpMtmWorkflowStepUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
        tmpMtmWorkflowStepUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getCreatorID()));
        tmpMtmWorkflowStepUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getUpdaterID()));
        result.add(tmpMtmWorkflowStepUserActor);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }


  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWorkflowStepUserActor tmpMtmWorkflowStepUserActor = (MtmWorkflowStepUserActor)((MtmWorkflowStepUserActor)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   MTM_WF_STEP_USER_ACTOR(ID, WORKFLOW_STEP_ID, ACTOR_TYPE, PERMISSION, ACTOR_ID, ACTION_TYPE, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpMtmWorkflowStepUserActor.getWorkflowStepID());
        setPrepareStatement(preStat, 3, tmpMtmWorkflowStepUserActor.getActorType());
        setPrepareStatement(preStat, 4, tmpMtmWorkflowStepUserActor.getPermission());
        setPrepareStatement(preStat, 5, tmpMtmWorkflowStepUserActor.getActorID());
        setPrepareStatement(preStat, 6, tmpMtmWorkflowStepUserActor.getActionType());
        if(GlobalConstant.RECORD_STATUS_PENDING.equals(tmpMtmWorkflowStepUserActor.getRecordStatus()) ){
          setPrepareStatement(preStat, 7, GlobalConstant.RECORD_STATUS_PENDING);
        } else {
          setPrepareStatement(preStat, 7, GlobalConstant.RECORD_STATUS_ACTIVE);
        }
        setPrepareStatement(preStat, 8, new Integer(0));
        setPrepareStatement(preStat, 9, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 10, currTime);
        setPrepareStatement(preStat, 11, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 12, currTime);
        preStat.executeUpdate();
        tmpMtmWorkflowStepUserActor.setID(nextID);
        tmpMtmWorkflowStepUserActor.setCreatorID(sessionContainer.getUserRecordID());
        tmpMtmWorkflowStepUserActor.setCreateDate(currTime);
        tmpMtmWorkflowStepUserActor.setUpdaterID(sessionContainer.getUserRecordID());
        tmpMtmWorkflowStepUserActor.setUpdateDate(currTime);
        tmpMtmWorkflowStepUserActor.setUpdateCount(new Integer(0));
        tmpMtmWorkflowStepUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getCreatorID()));
        tmpMtmWorkflowStepUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getUpdaterID()));
        return(tmpMtmWorkflowStepUserActor);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWorkflowStepUserActor tmpMtmWorkflowStepUserActor = (MtmWorkflowStepUserActor)((MtmWorkflowStepUserActor)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE MTM_WF_STEP_USER_ACTOR ");
        sqlStat.append("SET  WORKFLOW_STEP_ID=?, ACTOR_TYPE=?, PERMISSION=?, ACTOR_ID=?, ACTION_TYPE=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmWorkflowStepUserActor.getWorkflowStepID());
        setPrepareStatement(preStat, 2, tmpMtmWorkflowStepUserActor.getActorType());
        setPrepareStatement(preStat, 3, tmpMtmWorkflowStepUserActor.getPermission());
        setPrepareStatement(preStat, 4, tmpMtmWorkflowStepUserActor.getActorID());
        setPrepareStatement(preStat, 5, tmpMtmWorkflowStepUserActor.getActionType());
        setPrepareStatement(preStat, 6, new Integer(tmpMtmWorkflowStepUserActor.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 7, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 8, currTime);
        setPrepareStatement(preStat, 9, tmpMtmWorkflowStepUserActor.getID());
        setPrepareStatement(preStat, 10, tmpMtmWorkflowStepUserActor.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpMtmWorkflowStepUserActor.setUpdaterID(sessionContainer.getUserRecordID());
          tmpMtmWorkflowStepUserActor.setUpdateDate(currTime);
          tmpMtmWorkflowStepUserActor.setUpdateCount(new Integer(tmpMtmWorkflowStepUserActor.getUpdateCount().intValue() + 1));
          tmpMtmWorkflowStepUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getCreatorID()));
          tmpMtmWorkflowStepUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getUpdaterID()));
          return(tmpMtmWorkflowStepUserActor);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWorkflowStepUserActor tmpMtmWorkflowStepUserActor = (MtmWorkflowStepUserActor)((MtmWorkflowStepUserActor)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmWorkflowStepUserActor.getID());
        setPrepareStatement(preStat, 2, tmpMtmWorkflowStepUserActor.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpMtmWorkflowStepUserActor);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    MtmWorkflowStepUserActor tmpMtmWorkflowStepUserActor = (MtmWorkflowStepUserActor)this.oldValue;

    if (tmpMtmWorkflowStepUserActor != null) {
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getWorkflowStepID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getActorType()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getActorID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getActionType()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getUpdateDate()));
    }

    tmpMtmWorkflowStepUserActor = (MtmWorkflowStepUserActor)obj;
    if (tmpMtmWorkflowStepUserActor != null) {
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getWorkflowStepID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getActorType()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getActorID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getActionType()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepUserActor.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
  /**
   * Gets a list of pre-assigned step owners of the given step ID
   * 
   * @param workflowStepID The workflow step ID
   * @return List of pre-assigned step owners
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowStep
   * @see com.dcivision.workflow.bean.MtmWorkflowStepUserActor
   */
  public synchronized List getListByWorkflowStepID(Integer workflowStepID) throws ApplicationException {
    return this.getListByWorkflowStepID(workflowStepID, MtmWorkflowStepUserActor.ACTION_TYPE_EXECUTION);
  }
  
  /**
   * Gets a list of pre-assigned owners with types "Action Taker" or "Action Taker's Report To"
   * for the given workflow step
   * 
   * @param workflowStepID The workflow step ID
   * @return List of pre-assigned step owners
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowStep
   * @see com.dcivision.workflow.bean.MtmWorkflowStepUserActor#ACTOR_TYPE_ACTION_TAKER
   * @see com.dcivision.workflow.bean.MtmWorkflowStepUserActor#ACTOR_TYPE_REPORT_TO_OF_ACTION_TAKER
   */
  public synchronized List getActorTakerListWorkflowActorByWorkflowStepID(Integer workflowStepID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.ACTOR_TYPE, A.PERMISSION, A.ACTOR_ID, A.ACTION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR A ");
        sqlStat.append("WHERE  ( A.ACTOR_TYPE = ? OR A.ACTOR_TYPE = ? ) AND A.WORKFLOW_STEP_ID = ? ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, MtmWorkflowStepUserActor.ACTOR_TYPE_ACTION_TAKER);
        this.setPrepareStatement(preStat, 2, MtmWorkflowStepUserActor.ACTOR_TYPE_REPORT_TO_OF_ACTION_TAKER);
        this.setPrepareStatement(preStat, 3, workflowStepID);
        rs = preStat.executeQuery();
        
        while(rs.next()) {
        	MtmWorkflowStepUserActor tmpMtmWorkflowStepUserActor = new MtmWorkflowStepUserActor();
        	tmpMtmWorkflowStepUserActor.setID(getResultSetInteger(rs, "ID"));
        	tmpMtmWorkflowStepUserActor.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
        	tmpMtmWorkflowStepUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
          tmpMtmWorkflowStepUserActor.setPermission(getResultSetString(rs, "PERMISSION"));
        	tmpMtmWorkflowStepUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
        	tmpMtmWorkflowStepUserActor.setActionType(getResultSetString(rs, "ACTION_TYPE"));
        	tmpMtmWorkflowStepUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
        	tmpMtmWorkflowStepUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
        	tmpMtmWorkflowStepUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
        	tmpMtmWorkflowStepUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
        	tmpMtmWorkflowStepUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
        	tmpMtmWorkflowStepUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
        	tmpMtmWorkflowStepUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getCreatorID()));
        	tmpMtmWorkflowStepUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getUpdaterID()));
        	result.add(tmpMtmWorkflowStepUserActor);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a step owner which should be dynamic assigned for the given step
   *  
   * @param workflowStepID The workflow step ID
   * @return The step owner record correspond to dynamic assign
   * @throws ApplicationException
   */
  public synchronized MtmWorkflowStepUserActor getWorkflowStepUserActorByWorkflowStepID(Integer workflowStepID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();


    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.ACTOR_TYPE, A.PERMISSION, A.ACTOR_ID, A.ACTION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR A ");
        sqlStat.append("WHERE  A.ACTOR_TYPE = ? AND A.WORKFLOW_STEP_ID = ? ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, MtmWorkflowStepUserActor.ACTOR_TYPE_RUNTIME_ASSIGN);
        this.setPrepareStatement(preStat, 2, workflowStepID);
        rs = preStat.executeQuery();
        MtmWorkflowStepUserActor tmpMtmWorkflowStepUserActor = new MtmWorkflowStepUserActor();
        while(rs.next()) {

            tmpMtmWorkflowStepUserActor.setID(getResultSetInteger(rs, "ID"));
            tmpMtmWorkflowStepUserActor.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
            tmpMtmWorkflowStepUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
            tmpMtmWorkflowStepUserActor.setPermission(getResultSetString(rs, "PERMISSION"));
            tmpMtmWorkflowStepUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
            tmpMtmWorkflowStepUserActor.setActionType(getResultSetString(rs, "ACTION_TYPE"));

            tmpMtmWorkflowStepUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
            tmpMtmWorkflowStepUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
            tmpMtmWorkflowStepUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
            tmpMtmWorkflowStepUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
            tmpMtmWorkflowStepUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
            tmpMtmWorkflowStepUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
            tmpMtmWorkflowStepUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getCreatorID()));
            tmpMtmWorkflowStepUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getUpdaterID()));
        }
        return(tmpMtmWorkflowStepUserActor);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a list of pre-assigned step owners and of a certain action type
   * 
   * @param workflowStepID The workflow step ID
   * @param actionType The desired action type, null if ignore
   * @return List of pre-assigned step owners
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowStep
   */
  public synchronized List getListByWorkflowStepID(Integer workflowStepID, String actionType) throws ApplicationException {
    return getListByWorkflowStepID(workflowStepID,actionType,null,null);
  }
  
  public synchronized List getListByWorkflowStepID(Integer workflowStepID, String actionType,Integer filterBy,String[] submitterIDs) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        // For user group.
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.ACTOR_TYPE, A.PERMISSION, A.ACTOR_ID, A.ACTION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, ");
        sqlStat.append("       U.GROUP_NAME AS ACTOR_NAME ");
        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR A INNER JOIN USER_GROUP U ON A.ACTOR_ID = U.ID ");
        sqlStat.append("WHERE  A.ACTOR_TYPE = ? AND A.WORKFLOW_STEP_ID = ? AND (A.RECORD_STATUS = ? OR A.RECORD_STATUS = ? )");
        if (!"*".equals(actionType)) {
          sqlStat.append(" AND A.ACTION_TYPE = ? ");
        }

        // For user role.
        sqlStat.append("UNION ");
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.ACTOR_TYPE, A.PERMISSION, A.ACTOR_ID, A.ACTION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, ");
        sqlStat.append("       U.ROLE_NAME AS ACTOR_NAME ");
        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR A INNER JOIN USER_ROLE U ON A.ACTOR_ID = U.ID ");
        sqlStat.append("WHERE  A.ACTOR_TYPE = ? AND A.WORKFLOW_STEP_ID = ? AND (A.RECORD_STATUS = ? OR A.RECORD_STATUS = ? ) ");
        if (!"*".equals(actionType)) {
          sqlStat.append(" AND A.ACTION_TYPE = ? ");
        }

        // For user record, report to and submitter.
        sqlStat.append("UNION ");
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.ACTOR_TYPE, A.PERMISSION, A.ACTOR_ID, A.ACTION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, ");
        sqlStat.append("       U.FULL_NAME AS ACTOR_NAME ");
        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR A LEFT JOIN USER_RECORD U ON A.ACTOR_ID = U.ID ");
        
        //for filter by group/role/staff hierarchy
        if(!Utility.isEmpty(filterBy) && !Utility.isEmpty(submitterIDs)){
          if(new Integer(WorkflowStep.STEP_FILTERBY_GROUP).equals(filterBy)){
            sqlStat.append(" INNER JOIN MTM_USER_RECORD_USER_GROUP P ON P.USER_RECORD_ID = U.ID ");
          }else if(new Integer(WorkflowStep.STEP_FILTERBY_ROLE).equals(filterBy)){
            sqlStat.append(" INNER JOIN MTM_USER_RECORD_USER_ROLE R ON R.USER_RECORD_ID = U.ID ");
          }else{
            sqlStat.append(" INNER JOIN STAFF_RECORD S ON S.ID = U.USER_LINK_ID ");
          }       
        }
        
        sqlStat.append("WHERE  (A.ACTOR_TYPE = ? OR A.ACTOR_TYPE = ? OR A.ACTOR_TYPE = ? OR A.ACTOR_TYPE = ? OR A.ACTOR_TYPE = ? OR A.ACTOR_TYPE = ? OR A.ACTOR_TYPE = ? OR A.ACTOR_TYPE = ? OR A.ACTOR_TYPE = ? OR A.ACTOR_TYPE = ? OR A.ACTOR_TYPE = ?) AND A.WORKFLOW_STEP_ID = ? AND (A.RECORD_STATUS = ? OR A.RECORD_STATUS = ? )");
        if (!"*".equals(actionType)) {
          sqlStat.append(" AND A.ACTION_TYPE = ? ");
        }
        
        //for filter by group/role/staff hierarchy
        if(!Utility.isEmpty(filterBy) && !Utility.isEmpty(submitterIDs)){
          if(new Integer(WorkflowStep.STEP_FILTERBY_GROUP).equals(filterBy)){
            for(int i=0;i<submitterIDs.length;i++){
              if(i==0){
                sqlStat.append(" AND P.USER_GROUP_ID = "+submitterIDs[i]+" ");
              }else{
                sqlStat.append(" OR P.USER_GROUP_ID = "+submitterIDs[i]+" ");
              }
            }
          }else if(new Integer(WorkflowStep.STEP_FILTERBY_ROLE).equals(filterBy)){
            for(int i=0;i<submitterIDs.length;i++){
              if(i==0){
                sqlStat.append(" AND R.USER_ROLE_ID = "+submitterIDs[i]+" ");
              }else{
                sqlStat.append(" OR R.USER_ROLE_ID = "+submitterIDs[i]+" ");
              }
            }
          }else {
            for(int i=0;i<submitterIDs.length;i++){
              if(i==0){
                sqlStat.append(" AND S.LEVEL_" + filterBy.toString() + "_ID = "+submitterIDs[i]+" ");
              }else{
                sqlStat.append(" OR S.LEVEL_" + filterBy.toString() + "_ID = "+submitterIDs[i]+" ");
              }
            }
          }
        }
        
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int loop = 1;
        this.setPrepareStatement(preStat, loop++, MtmWorkflowStepUserActor.ACTOR_TYPE_GROUP);
        this.setPrepareStatement(preStat, loop++, workflowStepID);
        this.setPrepareStatement(preStat, loop++, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, loop++, GlobalConstant.RECORD_STATUS_PENDING);
        if (!"*".equals(actionType)) {
          this.setPrepareStatement(preStat, loop++, actionType);
        }
        this.setPrepareStatement(preStat, loop++, MtmWorkflowStepUserActor.ACTOR_TYPE_ROLE);
        this.setPrepareStatement(preStat, loop++, workflowStepID);
        this.setPrepareStatement(preStat, loop++, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, loop++, GlobalConstant.RECORD_STATUS_PENDING);
        if (!"*".equals(actionType)) {
          this.setPrepareStatement(preStat, loop++, actionType);
        }
        this.setPrepareStatement(preStat, loop++, MtmWorkflowStepUserActor.ACTOR_TYPE_USER);
        this.setPrepareStatement(preStat, loop++, MtmWorkflowStepUserActor.ACTOR_TYPE_ACTION_TAKER);
        this.setPrepareStatement(preStat, loop++, MtmWorkflowStepUserActor.ACTOR_TYPE_REPORT_TO);
        this.setPrepareStatement(preStat, loop++, MtmWorkflowStepUserActor.ACTOR_TYPE_SUBMITTER);
        this.setPrepareStatement(preStat, loop++, MtmWorkflowStepUserActor.ACTOR_TYPE_EVERYONE);
        this.setPrepareStatement(preStat, loop++, MtmWorkflowStepUserActor.ACTOR_TYPE_RUNTIME_ASSIGN);
        this.setPrepareStatement(preStat, loop++, MtmWorkflowStepUserActor.ACTOR_TYPE_RECURSIVE_RUNTIME_ASSIGN);
        this.setPrepareStatement(preStat, loop++, MtmWorkflowStepUserActor.ACTOR_TYPE_PREVIOUS_TASK_OWNER);
        this.setPrepareStatement(preStat, loop++, MtmWorkflowStepUserActor.ACTOR_TYPE_PREVIOUS_TASK_ACTION_TAKER);        
        this.setPrepareStatement(preStat, loop++, MtmWorkflowStepUserActor.ACTOR_TYPE_REPORT_TO_OF_PREVIOUS_TASK_ACTION_TAKER);
        this.setPrepareStatement(preStat, loop++, MtmWorkflowStepUserActor.ACTOR_TYPE_REPORT_TO_OF_ACTION_TAKER);
        this.setPrepareStatement(preStat, loop++, workflowStepID);
        this.setPrepareStatement(preStat, loop++, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, loop++, GlobalConstant.RECORD_STATUS_PENDING);
        if (!"*".equals(actionType)) {
          this.setPrepareStatement(preStat, loop++, actionType);
        }

        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowStepUserActor tmpMtmWorkflowStepUserActor = new MtmWorkflowStepUserActor();
          tmpMtmWorkflowStepUserActor.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepUserActor.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
          tmpMtmWorkflowStepUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
          tmpMtmWorkflowStepUserActor.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpMtmWorkflowStepUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
          tmpMtmWorkflowStepUserActor.setActionType(getResultSetString(rs, "ACTION_TYPE"));
          tmpMtmWorkflowStepUserActor.setActorName(getResultSetString(rs, "ACTOR_NAME"));
          tmpMtmWorkflowStepUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getCreatorID()));
          tmpMtmWorkflowStepUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getUpdaterID()));
          result.add(tmpMtmWorkflowStepUserActor);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Deletes the pre-assigned step owners of the given workflow step
   * 
   * @param workflowStepID The workflow step ID
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowStep
   */
  public void deleteListByWorkflowStepID(Integer workflowStepID) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR ");
        sqlStat.append("WHERE  WORKFLOW_STEP_ID=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, workflowStepID);
        updateCnt = preStat.executeUpdate();
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public  synchronized List getListWorkflowStepIDByActorIDAndActorType(Integer actorID,String actorType) throws ApplicationException {
    return getListWorkflowStepIDByActorIDAndActorTypeAndPermissions(actorID,actorType,null);
  }
  public  synchronized List getListWorkflowStepIDByActorIDAndActorTypeAndPermissions(Integer actorID,String actorType,String[] permissions) throws ApplicationException {
	    PreparedStatement preStat = null;
	    ResultSet rs = null;
	    StringBuffer sqlStat = new StringBuffer();
	    List result = new ArrayList();

	    synchronized(dbConn) {
	      try {
	        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.ACTOR_TYPE, A.PERMISSION, A.ACTOR_ID, A.ACTION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
	        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR A ");
	        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.Actor_ID = ? AND A.ACTOR_TYPE = ? ");
          if(!Utility.isEmpty(permissions)){
            sqlStat.append(" AND (");
            for(int y=0;y<permissions.length;y++){
              if(y==0){
                sqlStat.append(" A.PERMISSION LIKE ? ");
              }else{
                sqlStat.append(" OR A.PERMISSION LIKE ? ");
              }
            }
            sqlStat.append(" )");
          }
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
	        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
	        this.setPrepareStatement(preStat, 2, actorID);
	        this.setPrepareStatement(preStat, 3, actorType);
          if(!Utility.isEmpty(permissions)){
            for(int i=0;i<permissions.length;i++){
              String keyWord = this.getFormattedKeyword(permissions[i],"LIKE");
              this.setPrepareStatement(preStat, i+4, keyWord);
            }
          }
	        rs = preStat.executeQuery();
	        while (rs.next()) {
	        MtmWorkflowStepUserActor tmpMtmWorkflowStepUserActor = new MtmWorkflowStepUserActor();
	        tmpMtmWorkflowStepUserActor.setID(getResultSetInteger(rs, "ID"));
	        tmpMtmWorkflowStepUserActor.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
	        tmpMtmWorkflowStepUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
	        tmpMtmWorkflowStepUserActor.setPermission(getResultSetString(rs, "PERMISSION"));
	        tmpMtmWorkflowStepUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
	        tmpMtmWorkflowStepUserActor.setActionType(getResultSetString(rs, "ACTION_TYPE"));
	        tmpMtmWorkflowStepUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
	        tmpMtmWorkflowStepUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
	        tmpMtmWorkflowStepUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
	        tmpMtmWorkflowStepUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
	        tmpMtmWorkflowStepUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
	        tmpMtmWorkflowStepUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
	        tmpMtmWorkflowStepUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getCreatorID()));
	        tmpMtmWorkflowStepUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getUpdaterID()));
	        result.add(tmpMtmWorkflowStepUserActor);
	        }
	        return(result);
	      } catch (ApplicationException appEx) {
	        throw appEx;
	      } catch (SQLException sqle) {
	        log.error(sqle, sqle);
	        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
	      } catch (Exception e) {
	        log.error(e, e);
	        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
	      } finally {
	        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
	        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
	      }
	    }
	  }
  
  public  synchronized List getListByWorkflowStepIDAndActorIDAndActorTypeAndPermissions(Integer workflowStepID,Integer actorID,String actorType,String[] permissions) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.ACTOR_TYPE, A.PERMISSION, A.ACTOR_ID, A.ACTION_TYPE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.Actor_ID = ? AND A.ACTOR_TYPE = ? AND A.WORKFLOW_STEP_ID =? ");
        if(!Utility.isEmpty(permissions)){
          sqlStat.append(" AND (");
          for(int y=0;y<permissions.length;y++){
            if(y==0){
              sqlStat.append(" A.PERMISSION LIKE ? ");
            }else{
              sqlStat.append(" OR A.PERMISSION LIKE ? ");
            }
          }
          sqlStat.append(" )");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, actorID);
        this.setPrepareStatement(preStat, 3, actorType);
        this.setPrepareStatement(preStat, 4, workflowStepID);
        if(!Utility.isEmpty(permissions)){
          for(int i=0;i<permissions.length;i++){
            String keyWord = this.getFormattedKeyword(permissions[i],"LIKE");
            this.setPrepareStatement(preStat, i+5, keyWord);
          }
        }
        rs = preStat.executeQuery();
        while (rs.next()) {
        MtmWorkflowStepUserActor tmpMtmWorkflowStepUserActor = new MtmWorkflowStepUserActor();
        tmpMtmWorkflowStepUserActor.setID(getResultSetInteger(rs, "ID"));
        tmpMtmWorkflowStepUserActor.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
        tmpMtmWorkflowStepUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
        tmpMtmWorkflowStepUserActor.setPermission(getResultSetString(rs, "PERMISSION"));
        tmpMtmWorkflowStepUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
        tmpMtmWorkflowStepUserActor.setActionType(getResultSetString(rs, "ACTION_TYPE"));
        tmpMtmWorkflowStepUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
        tmpMtmWorkflowStepUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
        tmpMtmWorkflowStepUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
        tmpMtmWorkflowStepUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
        tmpMtmWorkflowStepUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
        tmpMtmWorkflowStepUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
        tmpMtmWorkflowStepUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getCreatorID()));
        tmpMtmWorkflowStepUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepUserActor.getUpdaterID()));
        result.add(tmpMtmWorkflowStepUserActor);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public  synchronized Integer getListCountByWorkflowStepID(Integer workflowStepID) throws ApplicationException {
    return  getListCountByWorkflowStepIDAndPermissions(workflowStepID,null);
  }
  
  public synchronized Integer getListCountByWorkflowStepIDAndPermissions(Integer workflowStepID,String[] permissions) throws ApplicationException{
    return getListCountByWorkflowStepIDAndActorTypeAndPermissions(workflowStepID,null,permissions);
  }
  
  public  synchronized Integer getListCountByWorkflowStepIDAndActorTypeAndPermissions(Integer workflowStepID,String actorType,String[] permissions) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT COUNT(*) AS LSCOUNT ");
        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.WORKFLOW_STEP_ID = ? ");
        if(!Utility.isEmpty(actorType)){
          sqlStat.append(" AND A.ACTOR_TYPE = ? ");
        }
        if(!Utility.isEmpty(permissions)){
          sqlStat.append(" AND (");
          for(int y=0;y<permissions.length;y++){
            if(y==0){
              sqlStat.append(" A.PERMISSION LIKE ? ");
            }else{
              sqlStat.append(" OR A.PERMISSION LIKE ? ");
            }
          }
          sqlStat.append(" )");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int index = 1;
        this.setPrepareStatement(preStat, index++, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, index++, workflowStepID);
        if(!Utility.isEmpty(actorType)){
          this.setPrepareStatement(preStat, index++, actorType);
        }
        if(!Utility.isEmpty(permissions)){
          for(int i=0;i<permissions.length;i++){
            String keyWord = this.getFormattedKeyword(permissions[i],"LIKE");
            this.setPrepareStatement(preStat, index++, keyWord);
          }
        }
        rs = preStat.executeQuery();
        if(rs.next()){
          return this.getResultSetInteger(rs, "LSCOUNT");
        }
        return new Integer(0);  
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public  synchronized Integer getListCountByWorkflowStepIDAndActorIDAndActorTypeAndPermissions(Integer workflowStepID,Integer actorID,String actorType,String[] permissions) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT COUNT(*) AS LSCOUNT ");
        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.WORKFLOW_STEP_ID = ? AND A.ACTOR_ID = ? AND A.ACTOR_TYPE = ? ");
        if(!Utility.isEmpty(permissions)){
          sqlStat.append(" AND (");
          for(int y=0;y<permissions.length;y++){
            if(y==0){
              sqlStat.append(" A.PERMISSION LIKE ? ");
            }else{
              sqlStat.append(" OR A.PERMISSION LIKE ? ");
            }
          }
          sqlStat.append(" )");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int index=1;
        this.setPrepareStatement(preStat, index++, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, index++, workflowStepID);
        this.setPrepareStatement(preStat, index++, actorID);
        this.setPrepareStatement(preStat, index++, actorType);
        if(!Utility.isEmpty(permissions)){
          for(int i=0;i<permissions.length;i++){
            String keyWord = this.getFormattedKeyword(permissions[i],"LIKE");
            this.setPrepareStatement(preStat, index++, keyWord);
          }
        }
        rs = preStat.executeQuery();
        if(rs.next()){
          return this.getResultSetInteger(rs, "LSCOUNT");
        }
        return new Integer(0);  
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public  synchronized Integer getListCountByActorIDAndActorTypeAndPermissions(Integer actorID,String actorType,String[] permissions) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT COUNT(*) AS LSCOUNT ");
        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ?  AND A.ACTOR_ID = ? AND A.ACTOR_TYPE = ? ");
        if(!Utility.isEmpty(permissions)){
          sqlStat.append(" AND (");
          for(int y=0;y<permissions.length;y++){
            if(y==0){
              sqlStat.append(" A.PERMISSION LIKE ? ");
            }else{
              sqlStat.append(" OR A.PERMISSION LIKE ? ");
            }
          }
          sqlStat.append(" )");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int index=1;
        this.setPrepareStatement(preStat, index++, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, index++, actorID);
        this.setPrepareStatement(preStat, index++, actorType);
        if(!Utility.isEmpty(permissions)){
          for(int i=0;i<permissions.length;i++){
            String keyWord = this.getFormattedKeyword(permissions[i],"LIKE");
            this.setPrepareStatement(preStat, index++, keyWord);
          }
        }
        rs = preStat.executeQuery();
        if(rs.next()){
          return this.getResultSetInteger(rs, "LSCOUNT");
        }
        return new Integer(0);  
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * this method only for workflow Step check
   * @param actorID
   * @param actorType
   * @param permissions
   * @return
   * @throws ApplicationException
   */
  public  synchronized List getListByParametersAndPermissions(String[] selectItem,Map parameterMap,String[] permissions) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List paramValue = new ArrayList();
    List result = new ArrayList();
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT DISTINCT ");
        for(int i=0;i<selectItem.length;i++){
          if(i>0){
            sqlStat.append(","); 
          }
          sqlStat.append(" A."+selectItem[i]+" ");
        }
        sqlStat.append("FROM   MTM_WF_STEP_USER_ACTOR A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ?  ");
        
        for(Iterator it=parameterMap.keySet().iterator();it.hasNext();){
          String key = (String)it.next();
          Object[] objArr = (Object[])parameterMap.get(key);
          sqlStat.append(" AND ( ");
          for(int i=0;i<objArr.length;i++){
            if(i>0){
              sqlStat.append(" OR ");
            }
            sqlStat.append("A."+key+" = ? ");
            paramValue.add(objArr[i]);
          }
          sqlStat.append(" ) "); 
        }
        if(!Utility.isEmpty(permissions)){
          sqlStat.append(" AND (");
          for(int y=0;y<permissions.length;y++){
            if(y==0){
              sqlStat.append(" A.PERMISSION LIKE ? ");
            }else{
              sqlStat.append(" OR A.PERMISSION LIKE ? ");
            }
          }
          sqlStat.append(" )");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int index=1;
        this.setPrepareStatement(preStat, index++, GlobalConstant.RECORD_STATUS_ACTIVE);
        
        if(!Utility.isEmpty(permissions)){
          for(int i=0;i<permissions.length;i++){
            String keyWord = this.getFormattedKeyword(permissions[i],"LIKE");
            this.setPrepareStatement(preStat, index++, keyWord);
          }
        }
        //set the parameter value
        
        index = this.setPreparedStatement(preStat,index++,paramValue);
        
        rs = preStat.executeQuery();
        while(rs.next()){
          Map valueMap = new HashMap();
          for(int i=0;i<selectItem.length;i++){
            String key = selectItem[i];
            valueMap.put(key, this.getResultSetString(rs, key));//return result as String
          }
          result.add(valueMap);
        }
        return result;
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
}